﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using FDEEPublicFunc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEEInterface
{
    public class TextPloter
    {
        public enum eTxtLocation
        {
            E_LEFT,
            E_RIGHT,
            E_TOP,
            E_BOTTOM
        }

        public enum ePileTxtLocation
        {
            E_LEFT_T,
            E_LEFT_B,
            E_RIGHT_T,
            E_RIGHT_B,
        }
        /// <summary>
        /// 带比例尺标题
        /// </summary>
        /// <param name="db"></param>
        /// <param name="Point">插入点坐标</param>
        /// <param name="text">文本</param>
        /// <param name="scaleString">比例尺文本</param>
        /// <param name="_scale">比例尺</param>
        /// <param name="tstyle">字体标准</param>
        /// <param name="isModel">是否是模型空间</param>
        public static void PrintScaleText(Database db, Point2d Point,double font1,double font2,
            string text, string scaleString,ref Extents2d ext, double _scale = 1, string tstyle ="En", bool isModel = true)
        {
            tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                if (isModel)
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                else
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                MText Mtext = new MText();
                Mtext.TextHeight = font1 * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                //Mtext.Width = 200 * _scale;

                DBText Title = new DBText()
                {
                    Height = font1 * _scale,
                    TextStyleId = st[tstyle],
                    TextString = text,
                    WidthFactor=1,
                    Position=Point.Convert3D(0,2*_scale),
                    HorizontalMode = TextHorizontalMode.TextCenter,
                    VerticalMode = TextVerticalMode.TextBase,
                    AlignmentPoint= Point.Convert3D(0, 2 * _scale),                    
                };
                

                
                double width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight) * _scale;
              
                //if (Mtext.ActualWidth / _scale > 80)
                //{
                //    width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight + 3) * _scale;
                //}
                //else if (Mtext.ActualWidth / _scale > 50)
                //{
                //    width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight + 2) * _scale;
                //}
                width = Mtext.ActualWidth;
                Mtext.Location = Point.Convert3D(-width/2, Mtext.TextHeight+2 * _scale);
                recorder.AppendEntity(Title);
                tr.AddNewlyCreatedDBObject(Title, true);

                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.Convert2D(), Mtext.Bounds.Value.MaxPoint.Convert2D()));
                Polyline line = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
                line.AddVertexAt(0, Point.Convert2D(-width / 2, 1 * _scale), 0, 0.5, 0.5);
                line.AddVertexAt(1, Point.Convert2D(width/2, 1 * _scale), 0, 0.5,0.5);
                line.LineWeight = LineWeight.LineWeight050;
                recorder.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                Extents2d ext0 = new Extents2d(line.Bounds.Value.MinPoint.Convert2D(), line.Bounds.Value.MaxPoint.Convert2D());
                Polyline line1 = new Polyline() { Closed = false, Layer = "细线" };//定义不封闭的Polyline 平面虚线
                line1.AddVertexAt(0, Point.Convert2D(-width / 2,  0), 0, 0, 0);
                line1.AddVertexAt(1, Point.Convert2D(width/2, 0), 0, 0, 0);
                line1.LineWeight = LineWeight.LineWeight020;
                recorder.AppendEntity(line1);
                tr.AddNewlyCreatedDBObject(line1, true);

                Extents2d ext1 = new Extents2d(line1.Bounds.Value.MinPoint.Convert2D(), line1.Bounds.Value.MaxPoint.Convert2D());


                DBText num = new DBText();
                num.TextString = scaleString;
                num.Height = font2 * _scale;
                num.Position = Point.Convert3D(width/2 + 1 * _scale, 1 * _scale);

                num.HorizontalMode = TextHorizontalMode.TextLeft;
                num.VerticalMode = TextVerticalMode.TextVerticalMid;
                num.AlignmentPoint = num.Position;

                num.TextStyleId = st[tstyle];
                num.WidthFactor = 1;
                num.Layer = "标注";
                recorder.AppendEntity(num);
                tr.AddNewlyCreatedDBObject(num, true);
                
               
                ext = ext.Add(ext0);
                ext = ext.Add(ext1);
                if (num.Bounds != null)
                {
                    Extents2d ext2 = new Extents2d(num.Bounds.Value.MinPoint.Convert2D(), num.Bounds.Value.MaxPoint.Convert2D());
                    ext = ext.Add(ext2);
                }
                tr.Commit();
            }
        }
        public static void PrintScaleTextNew(Database db, Point2d Point, string text, string scaleString, ref Extents2d ext, double _scale = 1, string tstyle = "En", bool isModel = true)
        {
            tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                if (isModel)
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                else
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                MText Mtext = new MText();
                Mtext.TextHeight = 4.0 * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                //Mtext.Width = 200 * _scale;

                DBText Title = new DBText()
                {
                    Height = 4.0 * _scale,
                    TextStyleId = st[tstyle],
                    TextString = text,
                    WidthFactor = 0.8,
                    Position = Point.Convert3D(0, 2 * _scale),
                    HorizontalMode = TextHorizontalMode.TextCenter,
                    VerticalMode = TextVerticalMode.TextBase,
                    AlignmentPoint = Point.Convert3D(0, 2 * _scale),
                };



                double width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight) * _scale;

                //if (Mtext.ActualWidth / _scale > 80)
                //{
                //    width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight + 3) * _scale;
                //}
                //else if (Mtext.ActualWidth / _scale > 50)
                //{
                //    width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight + 2) * _scale;
                //}
                width = Mtext.ActualWidth;
                Mtext.Location = Point.Convert3D(-width / 2, Mtext.TextHeight + 2 * _scale);
                recorder.AppendEntity(Title);
                tr.AddNewlyCreatedDBObject(Title, true);

                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.Convert2D(), Mtext.Bounds.Value.MaxPoint.Convert2D()));
                ext = ext.Add(new Extents2d(Title.Bounds.Value.MinPoint.Convert2D(), Title.Bounds.Value.MaxPoint.Convert2D()));
                Polyline line = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
                line.AddVertexAt(0, Point.Convert2D(-width / 2, 1 * _scale), 0, 0, 0);
                line.AddVertexAt(1, Point.Convert2D(width / 2, 1 * _scale), 0, 0, 0);
                line.LineWeight = LineWeight.LineWeight050;
                recorder.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                Extents2d ext0 = new Extents2d(line.Bounds.Value.MinPoint.Convert2D(), line.Bounds.Value.MaxPoint.Convert2D());
                Polyline line1 = new Polyline() { Closed = false, Layer = "细线" };//定义不封闭的Polyline 平面虚线
                line1.AddVertexAt(0, Point.Convert2D(-width / 2, 0), 0, 0, 0);
                line1.AddVertexAt(1, Point.Convert2D(width / 2, 0), 0, 0, 0);
                line1.LineWeight = LineWeight.LineWeight020;
                recorder.AppendEntity(line1);
                tr.AddNewlyCreatedDBObject(line1, true);

                Extents2d ext1 = new Extents2d(line1.Bounds.Value.MinPoint.Convert2D(), line1.Bounds.Value.MaxPoint.Convert2D());


                DBText num = new DBText();
                num.TextString = scaleString;
                num.Height = 2.5 * _scale;
                num.Position = Point.Convert3D(width / 2 + 1 * _scale, 1 * _scale);

                num.HorizontalMode = TextHorizontalMode.TextLeft;
                num.VerticalMode = TextVerticalMode.TextVerticalMid;
                num.AlignmentPoint = num.Position;

                num.TextStyleId = st[tstyle];
                num.WidthFactor = 0.8;
                num.Layer = "标注";
                recorder.AppendEntity(num);
                tr.AddNewlyCreatedDBObject(num, true);


                ext = ext.Add(ext0);
                ext = ext.Add(ext1);
                if (num.Bounds != null)
                {
                    Extents2d ext2 = new Extents2d(num.Bounds.Value.MinPoint.Convert2D(), num.Bounds.Value.MaxPoint.Convert2D());
                    ext = ext.Add(ext2);
                }
                tr.Commit();
            }
        }

        public static DBObjectCollection GetScaleTextDB(Point2d Point, string text, string scaleString, double _scale = 100)
        {
            DBObjectCollection res = new DBObjectCollection();
            //TextStyleTable st;
            //using (Transaction tr = db.TransactionManager.StartTransaction())
            //{
            //    st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
            //}

            MText Mtext = new MText();
            Mtext.TextHeight = 4.0 * _scale;
            Mtext.Contents = text;
            //Mtext.TextStyleId = st[tstyle];
            Mtext.Width = 200 * _scale;

            Mtext.Location = Point.Convert3D();
            double width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight) * _scale;
            if (Mtext.ActualWidth / _scale > 80)
            {
                width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight + 3) * _scale;
            }
            else if (Mtext.ActualWidth / _scale > 50)
            {
                width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight + 2) * _scale;
            }

            res.Add(Mtext);
            Polyline line = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
            line.AddVertexAt(0, Point.Convert2D(0, -Mtext.TextHeight - 1 * _scale), 0, 0, 0);
            line.AddVertexAt(1, Point.Convert2D(width, -Mtext.TextHeight - 1 * _scale), 0, 0, 0);
            line.LineWeight = LineWeight.LineWeight050;
            res.Add(line);
            Polyline line1 = new Polyline() { Closed = false, Layer = "细线" };//定义不封闭的Polyline 平面虚线
            line1.AddVertexAt(0, Point.Convert2D(0, -Mtext.TextHeight - 2 * _scale), 0, 0, 0);
            line1.AddVertexAt(1, Point.Convert2D(width, -Mtext.TextHeight - 2 * _scale), 0, 0, 0);
            line1.LineWeight = LineWeight.LineWeight020;
            res.Add(line1);

            DBText num = new DBText();
            num.TextString = scaleString;
            num.Height = 2.5 * _scale;
            num.Position = Point.Convert3D(width + 5 * _scale, -4 * _scale);

            num.HorizontalMode = TextHorizontalMode.TextCenter;
            num.VerticalMode = TextVerticalMode.TextVerticalMid;
            num.AlignmentPoint = num.Position;

            //num.TextStyleId = st[tstyle];
            num.WidthFactor = 0.7;
            num.Layer = "标注";
            res.Add(num);

            return res;
        }


        // tyg add 2021-1-26 begin
        public static DBObjectCollection PrintText(Database db,eTxtLocation eLocation, Point2d Point, string text, double _scale, string tstyle = "En", double Rotation = 0)
        {
            tstyle = Extensions.curFont;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                //BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                //BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                MText Mtext = new MText();
                Mtext.TextHeight = 4.0 * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                Mtext.Width = 200 * _scale;
                Mtext.Rotation = Rotation;

                switch (eLocation)
                {
                    case eTxtLocation.E_LEFT:
                        {
                            Mtext.Location = Point.Convert3D(-Mtext.ActualHeight - _scale,-Mtext.ActualWidth/2);
                            break;
                        }
                    case eTxtLocation.E_RIGHT:
                        {
                            Mtext.Location = Point.Convert3D(Mtext.ActualHeight- 2*_scale, -Mtext.ActualWidth / 2);
                            break;
                        }
                    case eTxtLocation.E_TOP:
                        {
                            Mtext.Location = Point.Convert3D(-Mtext.ActualWidth / 2, Mtext.ActualHeight+_scale);
                            break;
                        }
                    case eTxtLocation.E_BOTTOM:
                        {
                            Mtext.Location = Point.Convert3D(-Mtext.ActualWidth / 2, -Mtext.ActualHeight+ 2*_scale);
                            break;
                        }
                    default:
                        break;
                }
                res.Add(Mtext);
//                 recorder.AppendEntity(Mtext);
//                 tr.AddNewlyCreatedDBObject(Mtext, true);
                tr.Commit();
            }

            return res;
        }
        // tyg add 2021-1-26 begin

        public static DBObjectCollection PlotText(Database db,ref Extents2d ext, eTxtLocation eLocation, Point2d Point, string text, double _scale, string tstyle = "En", double Rotation = 0)
        {
            tstyle = Extensions.curFont;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                MText Mtext = new MText();
                Mtext.TextHeight = 2.5 * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                //Mtext.Width = 200 * _scale;
                Mtext.Rotation = Rotation;
                res.Add(Mtext);
                switch (eLocation)
                {
                    case eTxtLocation.E_LEFT:
                        {
                            Mtext.Location = Point.Convert3D(-Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.TopCenter;
                            break;
                        }
                    case eTxtLocation.E_RIGHT:
                        {
                            Mtext.Location = Point.Convert3D(Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.BottomCenter;
                            break;
                        }
                    case eTxtLocation.E_TOP:
                        {
                            Mtext.Location = Point.Convert3D(0, Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.TopCenter;
                            break;
                        }
                    case eTxtLocation.E_BOTTOM:
                        {
                            Mtext.Location = Point.Convert3D(0, -Mtext.ActualHeight);
                            Mtext.Attachment = AttachmentPoint.BottomCenter;
                            break;
                        }
                    default:
                        break;
                }

                recorder.AppendEntity(Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.Convert2D(), Mtext.Bounds.Value.MaxPoint.Convert2D()));
                tr.Commit();
                return res;
            } 
        }

        public static DBObjectCollection PlotText(Database db, ref Extents2d ext,  Point3d Point, string text, double _scale,double height=2, string tstyle = "En", double Rotation = 0)
        {
            tstyle = Extensions.curFont;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                DBText txt = new DBText();
                txt.Height = height * _scale;
                txt.TextString = text;
                txt.TextStyleId = st[tstyle];
                txt.Position = Point;
                //txt.AlignmentPoint = Point;
                txt.Rotation = Rotation;
                txt.LinetypeScale = 0.7;
                res.Add(txt);
                
                recorder.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.Convert2D(), txt.Bounds.Value.MaxPoint.Convert2D()));
                tr.Commit();
                return res;
            }
        }

        public static DBObjectCollection PlotTextMid(Database db, ref Extents2d ext, Point3d Point, string text, double _scale, double height = 2, string tstyle = "En", double Rotation = 0)
        {
            tstyle = Extensions.curFont;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                DBText txt = new DBText();
                txt.Height = height * _scale;
                txt.TextString = text;
                txt.TextStyleId = st[tstyle];
                txt.Position = Point;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.HorizontalMode = TextHorizontalMode.TextMid;
                txt.AlignmentPoint = Point;
                txt.Rotation = Rotation;
                txt.LinetypeScale = 0.7;
                res.Add(txt);

                recorder.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.Convert2D(), txt.Bounds.Value.MaxPoint.Convert2D()));
                tr.Commit();
                return res;
            }
        }

        public static DBObjectCollection PlotPaperText(Database db, Point3d Point, string text, double _scale, double height = 2, string tstyle = "En", double Rotation = 0)
        {
            tstyle = Extensions.curFont;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                DBText txt = new DBText();
                txt.Height = height * _scale;
                txt.TextString = text;
                txt.TextStyleId = st[tstyle];
                txt.Position = Point;
                //txt.AlignmentPoint = Point;
                txt.Rotation = Rotation;
                txt.LinetypeScale = 0.7;
                res.Add(txt);

                recorder.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);             
                tr.Commit();
                return res;
            }
        }
        public static void PrintPaperMText(Database db, Point3d Point, string text, double _scale, double height = 2, double len =57,string tstyle = "En", double Rotation = 0)
        {
            tstyle = Extensions.curFont;
           
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                MText Mtext = new MText();
                Mtext.TextHeight = height * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                Mtext.Width = len * _scale;
                Mtext.Rotation = Rotation;
                Mtext.Location = Point.Convert3D();

                recorder.AppendEntity(Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);

                tr.Commit();
            }
        }
        public static void PlotPileRebarText(Database db, ref Extents2d ext, Point3d Point, string text, double Rotation = 0, double len = 3.35,double txtCap=0, double _scale=1, double height = 2, string tstyle = "En")
        {
            tstyle = Extensions.curFont;
            //DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                double size = 3.35;
                PolylinePloter.AddPloy4(db, ref ext, Point.Convert2D(len * 0.5, len * 0.5), len * 0.5, len * 0.5, size, Rotation);

                //DBText
                DBText txt = new DBText();
                txt.Height = height * _scale;
                txt.TextString = text;
                txt.TextStyleId = st[tstyle];
                txt.Position = Point.Convert3D(0.5, txtCap);
                //txt.AlignmentPoint = Point;
          
                txt.Rotation = Rotation;
                txt.LinetypeScale = 0.7;
                //res.Add(txt);
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.HorizontalMode = TextHorizontalMode.TextLeft;
                txt.AlignmentPoint = Point.Convert3D(0.5, txtCap);
                recorder.AppendEntity(txt);

                tr.AddNewlyCreatedDBObject(txt, true);
                ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.Convert2D(), txt.Bounds.Value.MaxPoint.Convert2D()));
                tr.Commit();
                //return res;
            }
        }

        public static void DrawPileLeadText(Database db, Point2d Point, ePileTxtLocation eLocation, double len, string text, ref Extents2d ext, double _scale = 1, string tstyle = "Arial Narrow")
        {
            tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                MText txt1 = TextPloter.GetTextActualWidth(db, text, _scale, 2, Extensions.curFont);
                double width = txt1.ActualWidth;
                Polyline pl = new Polyline() { Closed = false, Layer = "细线"};
                double sc = 1.3;
                Point3d pt = Point.Convert3D(len+0.5, -len* sc + 2);
                switch (eLocation)
                {
                    case ePileTxtLocation.E_LEFT_T:
                        {
                            pl.AddVertexAt(0, Point.Convert2D(), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(-len, len * sc), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(-len- width-3* _scale, len * sc), 0, 0, 0);
                            pt = Point.Convert3D(-len - 0.5, len * sc + 2);
                            break;
                        }
                    case ePileTxtLocation.E_LEFT_B:
                        {
                            pl.AddVertexAt(0, Point.Convert2D(), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(-len, -len * sc), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(-len - width - 3 * _scale, -len * sc), 0, 0, 0);
                            pt = Point.Convert3D(-len-0.5, -len * sc + 2);
                            break;
                        }
                    case ePileTxtLocation.E_RIGHT_T:
                        {
                            pl.AddVertexAt(0, Point.Convert2D(), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(len, len * sc), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(len + width + 3 * _scale, len * sc), 0, 0, 0);
                            pt = Point.Convert3D(len + 1, len * sc + 2);
                            break;
                        }
                    case ePileTxtLocation.E_RIGHT_B:
                        {
                            pl.AddVertexAt(0, Point.Convert2D(), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(len, -len * sc), 0, 0, 0);
                            pl.AddVertexAt(0, Point.Convert2D(len + width + 3 * _scale, -len * sc), 0, 0, 0);
                            pt = Point.Convert3D(len + 1, -len * sc + 2);
                            break;
                        }
                    default:
                        pl.AddVertexAt(0, Point.Convert2D(), 0, 0, 0);
                        pl.AddVertexAt(0, Point.Convert2D(len, -len * sc), 0, 0, 0);
                        pl.AddVertexAt(0, Point.Convert2D(len + width + 3 * _scale, -len * sc), 0, 0, 0);
                        break;
                }
                PlotPileRebarText(db, ref ext, pt.Convert3D(0), text);
                recorder.AppendEntity(pl);
                tr.AddNewlyCreatedDBObject(pl, true);
                ext = ext.Add(new Extents2d(pl.Bounds.Value.MinPoint.Convert2D(), pl.Bounds.Value.MaxPoint.Convert2D()));
                tr.Commit();
            }
        }
        public static MText GetTextActualWidth(Database db, string text, double scale,
            double fontSize = 2, string tstyle = "En")
        {
            tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;

                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //DBText
                MText Mtext = new MText();
                Mtext.TextHeight = fontSize * scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
               

                return Mtext;
            }
        }

        public static void PrintMultiText(Database db, ref Extents2d ext, Point3d pt, string text, double scale, 
            double fontSize = 2.5,double width=50,bool isModel=true)
        {
            string tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                if(isModel)
                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                else
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
                MText Mtext = new MText();
                Mtext.TextHeight = fontSize * scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                Mtext.Location = pt.Convert3D(0/*Mtext.ActualWidth/2*/, -Mtext.ActualHeight / 2);
                Mtext.Attachment = AttachmentPoint.TopLeft;
                Mtext.Width = width * scale;
                Mtext.ColorIndex = 3;
                recorder.AppendEntity(Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.Convert2D(), Mtext.Bounds.Value.MaxPoint.Convert2D()));
                tr.Commit();
            }
        }


        /// <summary>
        /// 比例尺上下两侧文字
        /// </summary>
        /// <param name="db"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="scaleString"></param>
        /// <param name="ext"></param>
        /// <param name="_scale"></param>
        /// <param name="tstyle"></param>
        /// <param name="isModel"></param>
        public static void PrintScaleDoubleSideText(Database db,Point2d Point,double font, string text, string scaleString, ref Extents2d ext, double _scale = 1, string tstyle = "Arial Narrow", bool isModel = true)
        {
            tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                if (isModel)
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                else
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                MText Mtext = new MText();
                Mtext.TextHeight = font * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                //Mtext.Width = 200 * _scale;

                DBText Title = new DBText()
                {
                    Height = font * _scale,
                    TextStyleId = st[tstyle],
                    TextString = text,
                    WidthFactor = 1,
                    Position = Point.Convert3D(0, 2 * _scale),
                    HorizontalMode = TextHorizontalMode.TextCenter,
                    VerticalMode = TextVerticalMode.TextBase,
                    AlignmentPoint = Point.Convert3D(0, 2 * _scale),
                };

                double width = (Mtext.ActualWidth * 4 / Mtext.ActualHeight) * _scale;
                width = Mtext.ActualWidth;
                Mtext.Location = Point.Convert3D(-width / 2, Mtext.TextHeight + 2 * _scale);
                recorder.AppendEntity(Title);
                tr.AddNewlyCreatedDBObject(Title, true);

                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.Convert2D(), Mtext.Bounds.Value.MaxPoint.Convert2D()));
                Polyline line = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
                line.AddVertexAt(0, Point.Convert2D(-width / 2, 1 * _scale), 0, 0.5, 0.5);
                line.AddVertexAt(1, Point.Convert2D(width / 2, 1 * _scale), 0, 0.5, 0.5);
                line.LineWeight = LineWeight.LineWeight050;
                //recorder.AppendEntity(line);
                //tr.AddNewlyCreatedDBObject(line, true);
                Extents2d ext0 = new Extents2d(line.Bounds.Value.MinPoint.Convert2D(), line.Bounds.Value.MaxPoint.Convert2D());
                Polyline line1 = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
                line1.AddVertexAt(0, Point.Convert2D(-width / 2, 0), 0, 0, 0);
                line1.AddVertexAt(1, Point.Convert2D(width / 2, 0), 0, 0, 0);
                line1.LineWeight = LineWeight.LineWeight050;
                recorder.AppendEntity(line1);
                tr.AddNewlyCreatedDBObject(line1, true);

                Extents2d ext1 = new Extents2d(line1.Bounds.Value.MinPoint.Convert2D(), line1.Bounds.Value.MaxPoint.Convert2D());


                MText Mtext1 = new MText();
                Mtext1.TextHeight = font * _scale;
                Mtext1.Contents = scaleString;
                Mtext1.TextStyleId = st[tstyle];

                DBText num = new DBText();
                num.TextString = scaleString;
                num.Height = font * _scale;
                num.Position = Point.Convert3D(-Mtext1.ActualWidth/2,-num.Height);

                num.HorizontalMode = TextHorizontalMode.TextCenter;
                num.VerticalMode = TextVerticalMode.TextVerticalMid;
                num.AlignmentPoint = Point.Convert3D(0, -num.Height);

                num.TextStyleId = st[tstyle];
                num.WidthFactor = 1;
                num.Layer = "标注";
                recorder.AppendEntity(num);
                tr.AddNewlyCreatedDBObject(num, true);


                ext = ext.Add(ext0);
                ext = ext.Add(ext1);
                if (num.Bounds != null)
                {
                    Extents2d ext2 = new Extents2d(num.Bounds.Value.MinPoint.Convert2D(), num.Bounds.Value.MaxPoint.Convert2D());
                    ext = ext.Add(ext2);
                }
                tr.Commit();
            }
        }

        public static void PrintScaleDoubleSideTextNew(Database db, Point2d Point, double font, string text, string scaleString, ref Extents2d ext, double _scale = 1, string tstyle = "Arial Narrow", bool isModel = true)
        {
            tstyle = Extensions.curFont;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                if (isModel)
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                else
                    recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                MText Mtext = new MText();
                Mtext.TextHeight = font * _scale;
                Mtext.Contents = text;
                Mtext.TextStyleId = st[tstyle];
                //Mtext.Width = 200 * _scale;

                DBText Title = new DBText()
                {
                    Height = font * _scale,
                    TextStyleId = st[tstyle],
                    TextString = text,
                    WidthFactor = 1,
                    Position = Point.Convert3D(0, font * 0.5 * _scale),
                    HorizontalMode = TextHorizontalMode.TextCenter,
                    VerticalMode = TextVerticalMode.TextBase,
                    AlignmentPoint = Point.Convert3D(0, font * 0.5 * _scale),
                };

                double width = (Mtext.ActualWidth * font*1.1 / Mtext.ActualHeight) * _scale;
                width = Mtext.ActualWidth;
                Mtext.Location = Point.Convert3D(-width / 2, Mtext.TextHeight + font *0.5* _scale);
                recorder.AppendEntity(Title);
                tr.AddNewlyCreatedDBObject(Title, true);

                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.Convert2D(), Mtext.Bounds.Value.MaxPoint.Convert2D()));
                Polyline line = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
                line.AddVertexAt(0, Point.Convert2D(-width / 2, font*0.5 * _scale), 0, 0.5, 0.5);
                line.AddVertexAt(1, Point.Convert2D(width / 2, font * 0.5 * _scale), 0, 0.5, 0.5);
                line.LineWeight = LineWeight.LineWeight050;
                //recorder.AppendEntity(line);
                //tr.AddNewlyCreatedDBObject(line, true);
                Extents2d ext0 = new Extents2d(line.Bounds.Value.MinPoint.Convert2D(), line.Bounds.Value.MaxPoint.Convert2D());
                Polyline line1 = new Polyline() { Closed = false, Layer = "粗线" };//定义不封闭的Polyline 平面虚线
                line1.AddVertexAt(0, Point.Convert2D(-width / 2, 0), 0, 0, 0);
                line1.AddVertexAt(1, Point.Convert2D(width / 2, 0), 0, 0, 0);
                line1.LineWeight = LineWeight.LineWeight050;
                recorder.AppendEntity(line1);
                tr.AddNewlyCreatedDBObject(line1, true);

                Extents2d ext1 = new Extents2d(line1.Bounds.Value.MinPoint.Convert2D(), line1.Bounds.Value.MaxPoint.Convert2D());


                MText Mtext1 = new MText();
                Mtext1.TextHeight = font * _scale;
                Mtext1.Contents = scaleString;
                Mtext1.TextStyleId = st[tstyle];

                DBText num = new DBText();
                num.TextString = scaleString;
                num.Height = font * _scale;
                num.Position = Point.Convert3D(width / 2, font * 0.5 * _scale);

                num.HorizontalMode = TextHorizontalMode.TextLeft;
                num.VerticalMode = TextVerticalMode.TextVerticalMid;
                num.AlignmentPoint = Point.Convert3D(width / 2, font * 0.5 * _scale);

                num.TextStyleId = st[tstyle];
                num.WidthFactor = 1;
                num.Layer = "标注";
                recorder.AppendEntity(num);
                tr.AddNewlyCreatedDBObject(num, true);


                ext = ext.Add(ext0);
                ext = ext.Add(ext1);
                if (num.Bounds != null)
                {
                    Extents2d ext2 = new Extents2d(num.Bounds.Value.MinPoint.Convert2D(), num.Bounds.Value.MaxPoint.Convert2D());
                    ext = ext.Add(ext2);
                }
                tr.Commit();
            }
        }

        /// <summary>
        /// 增加引线
        /// </summary>
        /// <param name="anchor">标注点</param>
        /// <param name="textAnchor">文字左上角点</param>
        /// <param name="cont">文字内容</param>
        /// <param name="ext"></param>
        /// <param name="scale">比例</param>
        public static void CreateRibMLeader(Point2d pt, Point2d ptM, string cont,  ref Extents2d ext, double scale=1, double dis=2, string layer="细线")
        {
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary mlstyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead);
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;

                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                     OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { LayerId = lt[layer], Plinegen = true };
                Point2d p11 = ptM, p12 = ptM;
                MText txt1 = TextPloter.GetTextActualWidth(db, cont, scale, 2, Extensions.curFont);
                double width = txt1.ActualWidth;
                if (ptM.Y > pt.Y)
                {
                    if (ptM.X > pt.X)//左上
                    {
                        Extents2d extT = new Extents2d(ptM.Convert2D(2 * scale, 2 * scale), ptM.Convert2D(2 * scale, 2 * scale));
                        //TextPloter.AddDBTextWithCirle(ptM.Convert3D(2 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        PlotPileRebarText(db, ref extT, ptM.Convert3D(dis * scale, 2 * scale), cont);
                        p11 = ptM;
                        p12 = ptM.Convert2D(extT.W() + 3 * scale);
                    }
                    else//右上                  
                    {
                        Extents2d extT = new Extents2d(ptM.Convert2D(-2 * scale, 2 * scale), ptM.Convert2D(-2 * scale, 2 * scale));
                        //TextPloter.AddDBTextWithCirle(ptM.Convert3D(-18 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        PlotPileRebarText(db, ref extT, ptM.Convert3D(-width- dis * scale, 2 * scale), cont);
                        p11 = ptM;
                        p12 = ptM.Convert2D(-width - 2 * scale);
                    }
                }
                else
                {
                    if (ptM.X > pt.X)//右下
                    {
                        Extents2d extT = new Extents2d(ptM.Convert2D(2 * scale, 2 * scale), ptM.Convert2D(2 * scale, 2 * scale));
                        //TextPloter.AddDBTextWithCirle(ptM.Convert3D(2 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        PlotPileRebarText(db, ref extT, ptM.Convert3D(dis * scale, 2 * scale), cont);
                        p11 = ptM;
                        p12 = ptM.Convert2D(extT.W() + 3 * scale);
                    }
                    else//左下                  
                    {
                        Extents2d extT = new Extents2d(ptM.Convert2D(-2 * scale, 2 * scale), ptM.Convert2D(-2 * scale, 2 * scale));
                        //TextPloter.AddDBTextWithCirle(ptM.Convert3D(-18 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        PlotPileRebarText(db, ref extT, ptM.Convert3D(-width - dis * scale, 2 * scale), cont);
                        p11 = ptM;
                        p12 = ptM.Convert2D(-width - 2 * scale);
                    }
                }


                PL1.AddVertexAt(0, pt, 0, 0, 0);
                PL1.AddVertexAt(1, p11, 0, 0, 0);

                PL1.AddVertexAt(2, p12, 0, 0, 0);

             
                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);

                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.Convert2D(), PL1.Bounds.Value.MaxPoint.Convert2D()));

                tr.Commit();

            }

        }
    }
}

